import {router} from './index'
import * as u from '@/util/util'
import * as menuUtil from '@/util/menuUtil.js'

const whiteList = ['/login', '/']
let isAddRoute = false

//路由前置守卫
router.beforeEach((to, from, next) => {
    document.title = to.meta.title || '应用'
    const token = u.getToken()
    if (!whiteList.includes(to.path) && !token) {
        return next('/login')
    }
    // 创建动态路由，只有第一次创建
    if (!isAddRoute) {
        //创建动态路由
        menuUtil.createDynamicRoute()
        isAddRoute = true
        return next({...to, replace: true})
    }
    next()
})